home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Games / Pentominoes 2.0 / Shell ƒ / offscreen layer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-15  |  8.8 KB  |  281 lines  |  [TEXT/MMCC]

  1. #include "offscreen layer.h"
  2. #include "window layer.h"
  3. #include "timing.h"
  4.  
  5. extern    Boolean            gHasColorQD;    /* see environment.c */
  6.  
  7. MyOffscreenComboPtr AllocateOffscreenCombo(WindowRef theWindow, Rect *sourceRect, Rect *destRect,
  8.     short scrollSpeed, Handle theText, short font, short size)
  9. // leaks memory on error
  10. {
  11.     MyOffscreenComboPtr    offscreenComboPtr;
  12.     Rect            scrollSourceRect, scrollDestRect;
  13.     Boolean            dummy;
  14.     
  15.     offscreenComboPtr=(MyOffscreenComboPtr)NewPtr(sizeof(MyOffscreenComboRec));
  16.     offscreenComboPtr->sourceRect=*sourceRect;
  17.     offscreenComboPtr->destRect=*destRect;
  18.     offscreenComboPtr->destWindow=theWindow;
  19.     if ((offscreenComboPtr->offscreenWorldPtr=AllocatePartialOffscreenWorld(theWindow,
  20.         sourceRect, FALSE, &dummy))==0L)
  21.         return 0L;
  22.     if ((offscreenComboPtr->patternWorldPtr=AllocatePartialOffscreenWorld(theWindow,
  23.         sourceRect, FALSE, &dummy))==0L)
  24.         return 0L;
  25.     if ((offscreenComboPtr->scratchWorldPtr=AllocatePartialOffscreenWorld(theWindow,
  26.         sourceRect, FALSE, &dummy))==0L)
  27.         return 0L;
  28.     if ((scrollSpeed!=0) && (theText!=0L))
  29.     {
  30.         offscreenComboPtr->scrollSpeed=scrollSpeed;
  31.         offscreenComboPtr->theText=theText;
  32.         scrollSourceRect=*sourceRect;
  33.         scrollSourceRect.left+=scrollSpeed;
  34.         scrollDestRect=*sourceRect;
  35.         scrollDestRect.right-=scrollSpeed;
  36.         offscreenComboPtr->scrollSourceRect=scrollSourceRect;
  37.         offscreenComboPtr->scrollDestRect=scrollDestRect;
  38.         offscreenComboPtr->scrollXPos=sourceRect->right-sourceRect->left-1;
  39.         offscreenComboPtr->font=font;
  40.         offscreenComboPtr->size=size;
  41.         offscreenComboPtr->offset=0L;
  42.     }
  43.     else
  44.     {
  45.         offscreenComboPtr->scrollSpeed=0;
  46.         offscreenComboPtr->theText=0L;
  47.     }
  48.     
  49.     return offscreenComboPtr;
  50. }
  51.  
  52. MyOffscreenPtr AllocatePartialOffscreenWorld(WindowRef theWindow, Rect *boundsRect,
  53.     Boolean setPort, Boolean *depthChanged)
  54. {
  55.     long            offRowBytes, sizeOfOff;
  56.     PixMapHandle    thePixMapHandle;
  57.     GWorldPtr        theGWorld;
  58.     Ptr                bwBitMap;
  59.     GrafPort        bwGrafPort;
  60.     GrafPtr            bwGrafPtr;
  61.     OSErr            memError;
  62.     short            realWindowDepth, maxWindowDepth;
  63.     MyOffscreenPtr    offscreenWorldPtr;
  64.     
  65.     offscreenWorldPtr=(MyOffscreenPtr)NewPtr(sizeof(MyOffscreenRec));
  66.     if (offscreenWorldPtr==0L)
  67.         return 0L;
  68.     maxWindowDepth=GetWindowMaxDepth(theWindow);
  69.     realWindowDepth=GetWindowRealDepth(theWindow);
  70.     OffsetRect(boundsRect, -(boundsRect->left), -(boundsRect->top));
  71.     
  72.     *depthChanged=FALSE;
  73.     if (gHasColorQD)    /* w/o Color Quickdraw, GWorlds may not be supported */
  74.     {
  75.         /* try to create new graphics world; display error if unsuccessful */
  76.         if (NewGWorld(&theGWorld, (realWindowDepth>=maxWindowDepth) ? maxWindowDepth : 0,
  77.                 boundsRect, 0L, 0L, 0)!=0)
  78.         {
  79.             return 0L;
  80.         }
  81.         
  82.         NoPurgePixels(GetGWorldPixMap(theGWorld));    /* never purge our pixmap! */
  83.         
  84.         GetGWorld(&(offscreenWorldPtr->currentGWorld),
  85.             &(offscreenWorldPtr->currentGDHandle));    /* get current settings */
  86.         LockPixels(thePixMapHandle=GetGWorldPixMap(theGWorld));    /* important!  copybits may move mem */
  87.         /* update offscreen graphics world, compensating for change in pixel depth */
  88.         UpdateGWorld(&theGWorld, (realWindowDepth>=maxWindowDepth) ? maxWindowDepth : 0,
  89.                 boundsRect, 0L, 0L, 0);
  90.         if (setPort)
  91.             SetGWorld(theGWorld, 0L);                /* set to our offscreen gworld */
  92.         else
  93.             SetGWorld((GWorldPtr)offscreenWorldPtr->currentGWorld, offscreenWorldPtr->currentGDHandle);
  94.         
  95.         offscreenWorldPtr->offscreenPtr=(GrafPtr)theGWorld;
  96.         offscreenWorldPtr->pixMapHandle=thePixMapHandle;
  97.         offscreenWorldPtr->isColor=TRUE;
  98.  
  99.         if (((realWindowDepth>=maxWindowDepth) ? maxWindowDepth : realWindowDepth)!=GetWindowDepth(theWindow))
  100.         {
  101.             *depthChanged=TRUE;
  102.         }
  103.     }
  104.     else    /* deal with (guaranteed) B/W bitmaps manually */
  105.     {
  106.         bwGrafPtr=&bwGrafPort;
  107.         OpenPort(bwGrafPtr);
  108.         offRowBytes=(((boundsRect->right-boundsRect->left)+15)>>4)<<1;
  109.         sizeOfOff=(long)(boundsRect->bottom-boundsRect->top)*offRowBytes;
  110.         bwBitMap=NewPtr(sizeOfOff);
  111.         if (bwBitMap==0L)
  112.         {
  113.             memError=MemError();
  114.             ClosePort(bwGrafPtr);
  115.             return 0L;
  116.         }
  117.         
  118.         bwGrafPort.portBits.baseAddr=bwBitMap;
  119.         bwGrafPort.portBits.rowBytes=offRowBytes;
  120.         bwGrafPort.portBits.bounds=bwGrafPort.portRect=*boundsRect;
  121.         
  122.         if (setPort)
  123.             SetPort(bwGrafPtr);
  124.         else
  125.             SetPort(theWindow);
  126.         
  127.         offscreenWorldPtr->offscreenPtr=bwGrafPtr;
  128.         offscreenWorldPtr->bwBitMap=bwBitMap;
  129.         offscreenWorldPtr->bwGrafPort=bwGrafPort;
  130.         offscreenWorldPtr->isColor=FALSE;
  131.     }
  132.     
  133.     return offscreenWorldPtr;
  134. }
  135.  
  136. MyOffscreenPtr AllocateOffscreenWorld(WindowRef theWindow, Boolean *depthChanged)
  137. {
  138.     return AllocatePartialOffscreenWorld(theWindow, &(theWindow->portRect), TRUE, depthChanged);
  139. }
  140.  
  141. void CopybitsCombo(MyOffscreenComboPtr offscreenComboPtr)
  142. {
  143.     Rect            sourceRect, destRect;
  144.     GrafPtr            patternPtr;
  145.     GrafPtr            sourcePtr;
  146.     GrafPtr            comboPtr;
  147.     WindowRef        destWindow;
  148.     
  149.     sourceRect=offscreenComboPtr->sourceRect;
  150.     destRect=offscreenComboPtr->destRect;
  151.     destWindow=offscreenComboPtr->destWindow;
  152.     sourcePtr=offscreenComboPtr->offscreenWorldPtr->offscreenPtr;
  153.     patternPtr=offscreenComboPtr->patternWorldPtr->offscreenPtr;
  154.     comboPtr=offscreenComboPtr->scratchWorldPtr->offscreenPtr;
  155.  
  156.     CopyBits(&(sourcePtr->portBits), &(comboPtr->portBits), &sourceRect, &sourceRect, srcCopy, 0L);
  157.     CopyBits(&(patternPtr->portBits), &(comboPtr->portBits), &sourceRect, &sourceRect, srcOr, 0L);
  158.     CopyBits(&(comboPtr->portBits), &(destWindow->portBits), &sourceRect, &destRect, srcCopy, 0L);
  159. }
  160.  
  161. void ScrollTextCombo(MyOffscreenComboPtr offscreenComboPtr)
  162. {
  163.     unsigned char    theChar;
  164.     Rect            sourceRect;
  165.     Rect            destRect;
  166.     GrafPtr            patternPtr;
  167.     GrafPtr            sourcePtr;
  168.     GrafPtr            comboPtr;
  169.     WindowRef        destWindow;
  170.     short            charWidth;
  171.     
  172.     StartTiming();
  173.     
  174.     sourceRect=offscreenComboPtr->sourceRect;
  175.     destRect=offscreenComboPtr->destRect;
  176.     destWindow=offscreenComboPtr->destWindow;
  177.     sourcePtr=offscreenComboPtr->offscreenWorldPtr->offscreenPtr;
  178.     patternPtr=offscreenComboPtr->patternWorldPtr->offscreenPtr;
  179.     comboPtr=offscreenComboPtr->scratchWorldPtr->offscreenPtr;
  180.  
  181.     SetPortToOffscreenWindow(offscreenComboPtr->offscreenWorldPtr);
  182.     CopyBits(&(sourcePtr->portBits), &(sourcePtr->portBits), &(offscreenComboPtr->scrollSourceRect),
  183.              &(offscreenComboPtr->scrollDestRect), 0, 0L);
  184.     
  185.     MoveTo(sourceRect.left+(offscreenComboPtr->scrollXPos), sourceRect.bottom-3);
  186.     
  187.     theChar=(*(offscreenComboPtr->theText))[offscreenComboPtr->offset];
  188.     TextMode(notSrcCopy);
  189.     TextFont(offscreenComboPtr->font);
  190.     TextSize(offscreenComboPtr->size);
  191.     
  192.     charWidth=CharWidth(theChar);
  193.     DrawChar(theChar);
  194.     
  195.     SetPortToOnscreenWindow(offscreenComboPtr->destWindow, offscreenComboPtr->offscreenWorldPtr);
  196.     
  197.     CopyBits(&(sourcePtr->portBits), &(comboPtr->portBits), &sourceRect, &sourceRect, srcCopy, 0L);
  198.     CopyBits(&(patternPtr->portBits), &(comboPtr->portBits), &sourceRect, &sourceRect, srcOr, 0L);
  199.     CopyBits(&(comboPtr->portBits), &(destWindow->portBits), &sourceRect, &destRect, srcCopy, 0L);
  200.     
  201.     offscreenComboPtr->scrollXPos-=offscreenComboPtr->scrollSpeed;
  202.     if (offscreenComboPtr->scrollXPos+charWidth<=(sourceRect.right-sourceRect.left-1-offscreenComboPtr->scrollSpeed))
  203.     {
  204.         offscreenComboPtr->scrollXPos+=charWidth;
  205.         if ((++(offscreenComboPtr->offset))==GetHandleSize(offscreenComboPtr->theText))
  206.             offscreenComboPtr->offset=0L;
  207.     }
  208.     TimeCorrection(2);
  209. }
  210.  
  211. void ChangeDepthOffscreenWindow(WindowRef theWindow, MyOffscreenPtr *offscreenWorldPtr,
  212.     Boolean *depthChanged)
  213. {
  214.     short            realWindowDepth, maxWindowDepth;
  215.     GWorldPtr        theGWorld;
  216.     GWorldFlags        result;
  217.     
  218.     maxWindowDepth=GetWindowMaxDepth(theWindow);
  219.     realWindowDepth=GetWindowRealDepth(theWindow);
  220.     
  221.     *depthChanged=FALSE;
  222.     if ((*offscreenWorldPtr)->isColor)
  223.     {
  224.         SetPortToOnscreenWindow(theWindow, *offscreenWorldPtr);
  225.         theGWorld=(GWorldPtr)((*offscreenWorldPtr)->offscreenPtr);
  226.         result=UpdateGWorld(&theGWorld, (realWindowDepth>maxWindowDepth) ? maxWindowDepth : 0,
  227.             &(theWindow->portRect), 0L, 0L, 0);
  228.         *depthChanged=((result&newDepth)!=0) ? TRUE : FALSE;
  229.         (*offscreenWorldPtr)->offscreenPtr=(GrafPtr)theGWorld;
  230.         SetPortToOffscreenWindow(*offscreenWorldPtr);
  231.     }
  232. }
  233.  
  234. void SetPortToOnscreenWindow(WindowRef theWindow, MyOffscreenPtr offscreenWorldPtr)
  235. {
  236.     if (offscreenWorldPtr->isColor)
  237.     {
  238.         SetGWorld(offscreenWorldPtr->currentGWorld, offscreenWorldPtr->currentGDHandle);
  239.     }
  240.     
  241.     SetPort(theWindow);
  242. }
  243.  
  244. void SetPortToOffscreenWindow(MyOffscreenPtr offscreenWorldPtr)
  245. {
  246.     if (offscreenWorldPtr->isColor)
  247.     {
  248.         SetGWorld((GWorldPtr)offscreenWorldPtr->offscreenPtr, 0L);
  249.     }
  250.     else
  251.     {
  252.         SetPort(offscreenWorldPtr->offscreenPtr);
  253.     }
  254. }
  255.  
  256. MyOffscreenPtr DisposeOffscreenWorld(MyOffscreenPtr offscreenWorldPtr)
  257. {
  258.     if (offscreenWorldPtr->isColor)
  259.     {
  260.         UnlockPixels(offscreenWorldPtr->pixMapHandle);
  261.         DisposeGWorld((GWorldPtr)(offscreenWorldPtr->offscreenPtr));
  262.     }
  263.     else
  264.     {
  265.         ClosePort(offscreenWorldPtr->offscreenPtr);
  266.         DisposePtr(offscreenWorldPtr->bwBitMap);
  267.     }
  268.     
  269.     return 0L;
  270. }
  271.  
  272. MyOffscreenComboPtr DisposeOffscreenCombo(MyOffscreenComboPtr offscreenComboPtr)
  273. {
  274.     DisposeOffscreenWorld(offscreenComboPtr->offscreenWorldPtr);
  275.     DisposeOffscreenWorld(offscreenComboPtr->patternWorldPtr);
  276.     DisposeOffscreenWorld(offscreenComboPtr->scratchWorldPtr);
  277.     DisposePtr((Ptr)offscreenComboPtr);
  278.     
  279.     return 0L;
  280. }
  281.